package com.fyt.video.project;

import com.fyt.video.ClientDemo.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.thymeleaf.util.StringUtils;

import javax.swing.tree.DefaultMutableTreeNode;


public class MyClient{
    NativeLong lUserID;//用户句柄
    public static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public static HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo ;;
    public static String ipAddress = "192.168.1.230";
    public static String username = "admin";
    public static String password = "fyt12345";
    public static short port = 8000;
//    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public NativeLong login(){
        if (lUserID.intValue() > -1){
            //注销
            hCNetSDK.NET_DVR_Logout_V30(lUserID);
            lUserID = new NativeLong(-1);
        }
        //注册
        m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        lUserID = hCNetSDK.NET_DVR_Login_V30(ipAddress, port, username, password, m_strDeviceInfo);
        System.out.println("用户句柄："+lUserID);
        return lUserID;
    }

    public static void main(String[] args) {
        HCNetSDK.NET_DVR_IPPARACFG m_strIpparaCfg;//IP参数
        //初始化
        boolean initSuc = hCNetSDK.NET_DVR_Init();
        if(!initSuc){
            System.out.println("初始化失败");
            return;
        }
        //先注销
//        hCNetSDK.NET_DVR_Logout_V30(lUserID);
        //初始化成功之后注册
        NativeLong nativeLong = hCNetSDK.NET_DVR_Login_V30(ipAddress, port, username, password, m_strDeviceInfo);
        System.out.println(nativeLong);
        if (nativeLong.intValue() > -1){
            System.out.println("初始化成功");
        }else{
            System.out.println("初始化失败");
            return;
        }
        //获取通道
        IntByReference ibrBytesReturned = new IntByReference(0);//获取IP接入配置参数
        boolean bRet = false;
        int iChannelNum = -1;

        m_strIpparaCfg = new HCNetSDK.NET_DVR_IPPARACFG();
        m_strIpparaCfg.write();
        Pointer lpIpParaConfig = m_strIpparaCfg.getPointer();
        bRet = hCNetSDK.NET_DVR_GetDVRConfig(nativeLong, HCNetSDK.NET_DVR_GET_IPPARACFG, new NativeLong(0),
                lpIpParaConfig, m_strIpparaCfg.size(), ibrBytesReturned);
        m_strIpparaCfg.read();

        String devices = "";

        if (!bRet){
            //设备不支持,则表示没有IP通道
            for (int iChannum = 0; iChannum < m_strDeviceInfo.byChanNum; iChannum++) {
                devices = "Camera" + (iChannelNum + m_strDeviceInfo.byStartChan);
//                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Camera" + (iChannum + m_strDeviceInfo.byStartChan));
//                TreeModel.insertNodeInto(newNode, m_DeviceRoot, iChannum);
            }
        }else {
            //设备支持IP通道
            for (int iChannum = 0; iChannum < m_strDeviceInfo.byChanNum; iChannum++) {
                if (m_strIpparaCfg.byAnalogChanEnable[iChannum] == 1) {
                    devices = "IPCamera" + (iChannelNum + m_strDeviceInfo.byStartChan);
                }
            }
        }

        if (StringUtils.isEmpty(devices)){
            if (devices.charAt(0) == 'C'){//Camara开头表示模拟通道
                //子字符串中获取通道号
                iChannelNum = Integer.parseInt(devices.substring(6));
            }
        }else {
            if (devices.charAt(0) == 'I'){//IPCamara开头表示IP通道
                //子字符创中获取通道号,IP通道号要加32
                iChannelNum = Integer.parseInt(devices.substring(8)) + 32;
            }else {
                return;
            }
        }

    }
}
